{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "worthy-thumb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "plt.rcParams['font.family']='SimHei'\n",
    "plt.rcParams['axes.unicode_minus']= False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "becoming-assembly",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>group</th>\n",
       "      <th>landing_page</th>\n",
       "      <th>converted</th>\n",
       "      <th>date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>851104</td>\n",
       "      <td>2017-01-21 22:11:48.556739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>804228</td>\n",
       "      <td>2017-01-12 08:01:45.159739</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>661590</td>\n",
       "      <td>2017-01-11 16:55:06.154213</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>853541</td>\n",
       "      <td>2017-01-08 18:28:03.143765</td>\n",
       "      <td>treatment</td>\n",
       "      <td>new_page</td>\n",
       "      <td>0</td>\n",
       "      <td>2017-01-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>864975</td>\n",
       "      <td>2017-01-21 01:52:26.210827</td>\n",
       "      <td>control</td>\n",
       "      <td>old_page</td>\n",
       "      <td>1</td>\n",
       "      <td>2017-01-21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id                   timestamp      group landing_page  converted  \\\n",
       "0   851104  2017-01-21 22:11:48.556739    control     old_page          0   \n",
       "1   804228  2017-01-12 08:01:45.159739    control     old_page          0   \n",
       "2   661590  2017-01-11 16:55:06.154213  treatment     new_page          0   \n",
       "3   853541  2017-01-08 18:28:03.143765  treatment     new_page          0   \n",
       "4   864975  2017-01-21 01:52:26.210827    control     old_page          1   \n",
       "\n",
       "         date  \n",
       "0  2017-01-21  \n",
       "1  2017-01-12  \n",
       "2  2017-01-11  \n",
       "3  2017-01-08  \n",
       "4  2017-01-21  "
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('./ab_data.csv',sep=',')\n",
    "data['date'] = data.timestamp.str[:10]\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "understanding-enterprise",
   "metadata": {},
   "source": [
    "公式： $ n = p_0(1-p_0)\\left(\\frac{z_1-\\alpha + z_1-\\beta\\sqrt{\\frac{p(1-p)}{p_0(1-p_0)}}}{p-p_0}\\right)^2 $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "southeast-breathing",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 根据常规默认值确定α ,β,K 值  α = 0.05,  β = 0.2\n",
    "alpha = 0.05\n",
    "beta = 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "distributed-sequence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.6448536269514729 0.8416212335729142\n"
     ]
    }
   ],
   "source": [
    "z_alpha = stats.norm.isf(alpha,loc=0,scale=1)\n",
    "z_beta = stats.norm.isf(beta,loc=0,scale=1)\n",
    "print(z_alpha,z_beta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "excess-entry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1203863045004612"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p0：对照组，oldpage的点击率\n",
    "co_p = data.converted[(data.group == 'control')&(data.landing_page == 'old_page')].mean()\n",
    "co_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "greenhouse-system",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.10589344218918344"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p0*(1-P0)\n",
    "num1 = co_p*(1-co_p)\n",
    "num1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "blank-patrick",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.11338571609917422"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p：实验结果发生的概率，此处为假设值\n",
    "p = co_p + 0.01\n",
    "num2 = p*(1-p)\n",
    "num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "excellent-intro",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6701.938803160933"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 套用公式，求n\n",
    "n = num1*((z_alpha+z_beta*np.sqrt(num2/num1))/0.01)**2\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "played-tiffany",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "group      landing_page\n",
       "control    new_page          1928\n",
       "           old_page        145274\n",
       "treatment  new_page        145311\n",
       "           old_page          1965\n",
       "Name: user_id, dtype: int64"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby(['group','landing_page'])['user_id'].count()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
